package cn.lirenyi.order.service.impl;

import cn.lirenyi.basic.service.impl.BaseServiceImpl;
import cn.lirenyi.basic.util.CodeGenerateUtils;
import cn.lirenyi.basic.util.PageList;
import cn.lirenyi.order.constants.OrderConstants;
import cn.lirenyi.order.domain.AdoptOrder;
import cn.lirenyi.order.domain.OrderAddress;
import cn.lirenyi.order.mapper.AdoptOrderMapper;
import cn.lirenyi.order.mapper.OrderAddressMapper;
import cn.lirenyi.order.query.AdoptOrderQuery;
import cn.lirenyi.order.service.IAdoptOrderService;
import cn.lirenyi.org.domain.Employee;
import cn.lirenyi.org.mapper.EmployeeMapper;
import cn.lirenyi.pay.constants.PayConstants;
import cn.lirenyi.pay.domain.PayBill;
import cn.lirenyi.pay.mapper.PayBillMapper;
import cn.lirenyi.pay.service.IPayBillService;
import cn.lirenyi.pet.domain.Pet;
import cn.lirenyi.pet.mapper.PetMapper;
import cn.lirenyi.user.domain.Logininfo;
import cn.lirenyi.user.domain.User;
import cn.lirenyi.user.domain.UserAddress;
import cn.lirenyi.user.mapper.UserAddressMapper;
import cn.lirenyi.user.mapper.UserMapper;
import com.sun.org.apache.bcel.internal.classfile.Code;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class AdoptOrderServiceImpl extends BaseServiceImpl<AdoptOrder> implements IAdoptOrderService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private AdoptOrderMapper adoptOrderMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private IPayBillService iPayBillService;


    @Override
    @Transactional
    public String submit(Map<String, Object> params, Logininfo logininfo) {
        // 校验参数
        // 送货方式
        Long serviceMethod = Long.valueOf(params.get("service_method").toString());
        // 收货地址
        Long addressId = Long.valueOf(params.get("address_id").toString()) ;
        // 支付方式 1支付宝    2微信     0银联
        Long payMethod =  Long.valueOf(params.get("pay_method").toString());
        Long petId = Long.valueOf(params.get("pet_id").toString());

        // 修改宠物状态
        Pet pet = petMapper.queryById(petId);
        User user = userMapper.queryLoginInfoId(logininfo.getId());
        // 绑定领养人
        pet.setUser_id(user.getId());
        // 下架时间
        pet.setOffsaletime(new Date());
        pet.setState(0);
        petMapper.update(pet);

        // 创建领养订单
        // 订单表
        AdoptOrder order = initAdoptOrder(pet, user);
        // 为了方便支付订单取值，先把唯一标识设置好，下面支付订单直接取值
        order.setPaySn(CodeGenerateUtils.generateUnionPaySn());
        // 返回自增id
        adoptOrderMapper.add(order);

        // 绑定用户地址
        UserAddress userAddress = userAddressMapper.queryById(addressId);
        OrderAddress orderAddress = initOrderAddress(order, userAddress);
        orderAddressMapper.add(orderAddress);

        //  支付订单并且支付
        PayBill bill = initPayBill(payMethod, user, order);
        payBillMapper.add(bill);

        // 为了方便被后续使用，使用支付模块提供一个接口来支付
        // 返回支付接口的响应数据包，即需要响应给前台的form表单
        return iPayBillService.pay(bill);


        // @todo 支付订单超时自动取消
       // System.out.println("该订单的金额是："+order.getPrice());
    }

    private PayBill initPayBill(Long payMethod, User user, AdoptOrder order) {
        PayBill bill = new PayBill();
        // 设置订单的详情
        bill.setDigest(order.getDigest());
        bill.setMoney(order.getPrice());
        bill.setUnionPaySn(order.getPaySn());
        bill.setLastPayTime(new Date(System.currentTimeMillis() + OrderConstants.LAST_PAY_TIME));
        // 支付方式来自用户自己选，上面接收了参数，可以直接用
        bill.setPayChannel(payMethod);
        bill.setBusinessType(PayConstants.BUSINESS_TYPE_ADOPT);
        bill.setBusinessKey(order.getId());
        bill.setUser_id(order.getUser_id());
        bill.setShop_id(order.getShop_id());
        bill.setNickName(user.getUsername());
        return bill;
    }

    @Override
    public PageList<AdoptOrder> admin(AdoptOrderQuery query, Logininfo logininfo) {
        Employee employee = employeeMapper.queryLoginInfoId(logininfo.getId());
        if(employee.getShop_id()!= null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public PageList<AdoptOrder> user(AdoptOrderQuery query, Logininfo logininfo) {
        User user = userMapper.queryLoginInfoId(logininfo.getId());
        query.setUserId(user.getId());
        return super.queryPage(query);
    }

    private OrderAddress initOrderAddress(AdoptOrder order, UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress, orderAddress);
        orderAddress.setOrder_id(order.getId());
        orderAddress.setOrderSn(order.getOrderSn());
        return orderAddress;
    }

    private AdoptOrder initAdoptOrder(Pet pet, User user) {
        AdoptOrder order = new AdoptOrder();
        order.setDigest("[摘要]对"+pet.getName()+"领养订单！");
        order.setPrice(pet.getSaleprice());
        String orderSn = CodeGenerateUtils.generateOrderSn(user.getId());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(user.getId());
        order.setShop_id(pet.getShop_id());
        // 设置支付超时自动取消
        order.setLastPayTime(new Date(System.currentTimeMillis() + OrderConstants.LAST_PAY_TIME));
        return order;
    }
}
